package com.smartalarmclock.UI;

import java.util.Map.Entry;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IAlarmClock;
import com.smartalarmclock.Model.IAutoResponse;
import com.smartalarmclock.Model.IContact;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ISMSMessage;
import com.smartalarmclock.Model.SMSMessageFactory;
import com.smartalarmclock.Model.Exceptions.DataEntityDoesNotExistException;
import com.smartalarmclock.Model.Exceptions.InvalidSaveDataEntityOperationException;
import com.smartalarmclock.Model.IAlarmClock.ShutOffMethod;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class AutoResponseSettingsActivity extends Activity {
	public enum GameToPlay {None, MathEquation, AnswerPhrase}

	private static final String TAG = "AutoResponseActivity";
	private IProfile m_Profile = null;
	private boolean m_AutoResponseMatching = false;
	
	private boolean m_BlockCalls = false;
	private GameToPlay m_GameToPlay = GameToPlay.None;
	private boolean m_AlarmClockActivated = false;
	private boolean m_SmsResponseEnabled = false;
	
	// Buttons in page:
	RadioButton m_SmsEnabled;
	RadioButton m_SmsDisabled;
	
	TextView m_SmsEnabledText;
	TextView m_SmsMessageText;
	EditText m_SmsMessage;
	
	TextView m_GameTextView;
	RadioButton m_GameNone;
	RadioButton m_GameMath;
	RadioButton m_GamePhrase;
	
	RadioButton m_BlockingEnabled;
	RadioButton m_BlockingDisabled;
	
	RadioButton m_AlarmClockRadioEnabled;
	RadioButton m_AlarmClockRadioDisabled;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.set_auto_response_screen);
	    Bundle bundle = this.getIntent().getExtras();
	    
	    int profileId = bundle.getInt(Messages.PROFILE_DETAILS_PROFILE_ID);
    	m_Profile = AcManager.GetInstance().GetProfileDetails(this, profileId);
    	m_AutoResponseMatching = bundle.getBoolean(Messages.AUTO_RESPONSE_MATCHING);
    	
	    final Button buttonSave = (Button)findViewById(R.id.button_save_auto_response_settings);
	    buttonSave.setOnClickListener(new OnClickSaveListener(this));
	    setOriginalValues();
	    refreshAutoResponseCurrentOptions();
	}
	
	private class OnClickSaveListener implements OnClickListener {

		private Context m_Context = null;
		public OnClickSaveListener(Context context)
		{
			m_Context = context;
		}
		
		@Override
        public void onClick(View v)
        {
			Log.v(TAG, "Trying to save auto_response");
			IAutoResponse autoResponse = m_AutoResponseMatching ? m_Profile.GetMatchingAutoResponse() : m_Profile.GetNonMatchingAutoResponse();
			
			autoResponse.SetBlocking(m_BlockCalls);
			if (m_SmsResponseEnabled)
			{
			    final EditText editTextSmsResponse = (EditText)findViewById(R.id.editText_sms_response);

				SMSMessageFactory smsFactory = new SMSMessageFactory();
				ISMSMessage smsMessageExist = null;
				try {
					smsMessageExist = smsFactory.GetSMSMessageByMessageString(m_Context, editTextSmsResponse.getText().toString());
				} catch (DataEntityDoesNotExistException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ISMSMessage smsMessage;
				if (smsMessageExist == null)
				{
					smsMessage = smsFactory.CreateSMSMessage(editTextSmsResponse.getText().toString());
					autoResponse.SetSmsToSend(smsMessage);
				}
			}
			else
			{
				autoResponse.SetSmsToSend(null);
			}
			
			try {
				autoResponse.Save(m_Context);
			} catch (InvalidSaveDataEntityOperationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			IAlarmClock alarmClock = m_Profile.GetAlarmClock();
			alarmClock.SetRingStatus(m_AlarmClockActivated);
			if (m_AlarmClockActivated)
			{
				switch(m_GameToPlay)
				{
					case AnswerPhrase:
						alarmClock.SetShutOffMethod(ShutOffMethod.SentenceTyping);
						break;
					case MathEquation:
						alarmClock.SetShutOffMethod(ShutOffMethod.MathEquation);
						break;
					case None:
						alarmClock.SetShutOffMethod(ShutOffMethod.Snooze);
						break;
				}
				
			}
			
			try {
				alarmClock.Save(m_Context);
			} catch (InvalidSaveDataEntityOperationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
			// update the profile
			AcManager.GetInstance().ModifyProfile(m_Context, m_Profile);
			
			// exit the activity
			finish();
		}
	};
	
	public void refreshAutoResponseCurrentOptions()
	{		
		boolean blockEnabled = m_BlockCalls == true; 
		m_BlockingEnabled.setChecked(blockEnabled);
		m_BlockingDisabled.setChecked(!blockEnabled);
		
		m_SmsResponseEnabled = m_BlockCalls ? m_SmsResponseEnabled : false;
		
		m_SmsEnabled.setChecked(m_SmsResponseEnabled);
		m_SmsDisabled.setChecked(!m_SmsResponseEnabled);
		
		int smsEnabledDisplay = m_BlockCalls ? View.VISIBLE : View.GONE;
		m_SmsEnabled.setVisibility(smsEnabledDisplay);
		m_SmsDisabled.setVisibility(smsEnabledDisplay);
		
		m_SmsEnabledText.setVisibility(smsEnabledDisplay);
		
		int smsMessageDisplay = m_SmsResponseEnabled == false ? View.GONE : View.VISIBLE;
		m_SmsMessageText.setVisibility(smsMessageDisplay);
		m_SmsMessage.setVisibility(smsMessageDisplay);
		
		int chooseGameDisplay = m_AlarmClockActivated == false ? View.GONE : View.VISIBLE;
		m_GameTextView.setVisibility(chooseGameDisplay);
		m_GameNone.setVisibility(chooseGameDisplay);
		m_GameMath.setVisibility(chooseGameDisplay);
		m_GamePhrase.setVisibility(chooseGameDisplay);
	}
	
	private void setOriginalValues()
	{
		m_BlockingEnabled = (RadioButton)findViewById(R.id.radio_block_calls);
		m_BlockingDisabled = (RadioButton)findViewById(R.id.radio_dont_block_calls);
		
		m_SmsEnabled = (RadioButton)findViewById(R.id.radio_auto_sms_enabled);
		m_SmsDisabled = (RadioButton)findViewById(R.id.radio_auto_sms_disabled);
		
		m_SmsEnabledText = (TextView)findViewById(R.id.textView_send_auto_sms);
		m_SmsMessageText = (TextView)findViewById(R.id.textView_sms_response);
		m_SmsMessage = (EditText)findViewById(R.id.editText_sms_response);
		
		m_GameTextView = (TextView)findViewById(R.id.textView_play_game);
		m_GameNone = (RadioButton)findViewById(R.id.radio_no_game);
		m_GameMath = (RadioButton)findViewById(R.id.radio_answer_equation);
		m_GamePhrase = (RadioButton)findViewById(R.id.radio_write_phrase);
		m_AlarmClockRadioEnabled = (RadioButton)findViewById(R.id.radio_alarm_clock_enabled);
		m_AlarmClockRadioDisabled = (RadioButton)findViewById(R.id.radio_alarm_clock_disabled);
		
		IAutoResponse autoResponse = m_AutoResponseMatching ? m_Profile.GetMatchingAutoResponse() : m_Profile.GetNonMatchingAutoResponse();
		m_BlockCalls = autoResponse.IsBlocking();
		m_SmsResponseEnabled = autoResponse.IsSendingAutoSMS();
		m_AlarmClockActivated = m_Profile.GetAlarmClock().IsRinging();
		m_AlarmClockRadioEnabled.setChecked(m_AlarmClockActivated);
		m_AlarmClockRadioDisabled.setChecked(!m_AlarmClockActivated);
		
		switch(m_Profile.GetAlarmClock().GetShutOffMethod())
		{
		case MathEquation:
			m_GameToPlay = GameToPlay.MathEquation;
			m_GameMath.setChecked(true);
		case SentenceTyping:
			m_GameToPlay = GameToPlay.AnswerPhrase;
			m_GamePhrase.setChecked(true);
		case Snooze:
			m_GameToPlay = GameToPlay.None;
			m_GameNone.setChecked(true);
		}
		
		if (m_SmsResponseEnabled)
		{
			m_SmsMessage.setText(autoResponse.GetAutoSMS().GetMessage());
		}
		else
		{
			m_SmsMessage.setText("");
		}
	}
	
	public void onRadioButtonBlockClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_block_calls:
	            if (checked)
	            {
	            	m_BlockCalls = true;
	            }
	            break;
	        case R.id.radio_dont_block_calls:
	            if (checked)
	            {
	            	m_BlockCalls = false;
	            }
	            break;
	    }
	    
	    refreshAutoResponseCurrentOptions();
	}
	
	public void onRadioButtonGameClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_no_game:
	            if (checked)
	            {
	            	m_GameToPlay = GameToPlay.None;
	            }
	            break;
	        case R.id.radio_write_phrase:
	            if (checked)
	            {
	            	m_GameToPlay = GameToPlay.AnswerPhrase;
	            }
	            break;
	        case R.id.radio_answer_equation:
	            if (checked)
	            {
	            	m_GameToPlay = GameToPlay.MathEquation;
	            }
	            break;
	    }
	    
	    refreshAutoResponseCurrentOptions();
	}
	
	public void onRadioButtonAlarmClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_alarm_clock_enabled:
	            if (checked)
	            {
	            	m_AlarmClockActivated = true;
	            }
	            break;
	        case R.id.radio_alarm_clock_disabled:
	            if (checked)
	            {
	            	m_AlarmClockActivated = false;
	            }
	            break;
	    }
	    
	    refreshAutoResponseCurrentOptions();
	}
	
	public void onRadioButtonSmsSendClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_auto_sms_enabled:
	            if (checked)
	            {
	            	m_SmsResponseEnabled = true;
	            }
	            break;
	        case R.id.radio_auto_sms_disabled:
	            if (checked)
	            {
	            	m_SmsResponseEnabled = false;
	            }
	            break;
	    }
	    
	    refreshAutoResponseCurrentOptions();
	}

}
